package com.ilongsay.service.impl;

import com.ilongsay.dao.BillSqliteDao;
import com.ilongsay.dao.BillSqliteDetailDao;
import com.ilongsay.entity.AjaxObject;
import com.ilongsay.entity.FaPiao;
import com.ilongsay.entity.FaPiaoDetail;
import com.ilongsay.service.IMergeService;
import com.ilongsay.utils.CalcUtils;
import com.ilongsay.utils.ConfirmUtils;
import com.ilongsay.utils.DateUtils;
import com.ilongsay.utils.UuidUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @Author ilongsay
 * @Email ilongsay@163.com
 * @Copyright 版本所有，侵权必究！
 * @Datetime 2019-01-03 15:14
 * @Describution
 */
@Service("mergeService")
public class MergeServiceImpl implements IMergeService {

    @Autowired
    private BillSqliteDetailDao billSqliteDetailDao;

    @Autowired
    private BillSqliteDao billSqliteDao;

    private static final String MSG_OK = "合并成功，请检查合并后的数据是否正确！";
    private static final String MSG_ERROR = "合并失败，原因：";


    public AjaxObject mergeBillCode1(String billCodes) {
        AjaxObject ajaxObject = new AjaxObject();

        //合并后的订单号的生成规则
        String date = DateUtils.date();
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(date);
        stringBuilder.append("HB");

        List<FaPiao> faPiaos = new ArrayList<>();
        List<FaPiaoDetail> faPiaoDetails = new ArrayList<>();
        FaPiao faPiao = null;
        String total = "0";
        String untotal = "0";
        String taxAmount = "0";

        String[] split = billCodes.split("\\,");
        for (String billCode : split) {
            faPiaos = billSqliteDao.findBillByBillCode(billCode);//一个订单只能查询出一张主表的单据信息
            billSqliteDao.deleteBillByBillCode(billCode);//删除主表数据，sqlite级联删除有问题！
            billSqliteDetailDao.deleteBillDetailsByBillCode(billCode);//删除明细表数据
            for (FaPiaoDetail faPiaoDetail : faPiaos.get(0).getFaPiaoDetails()) {
                faPiaoDetail.setBillCode(stringBuilder.toString());//设置明细的订单号
                faPiaoDetail.setUpid(UuidUtils.randomString());//设置合并后的明细的订单号
            }
            faPiaoDetails.addAll(faPiaos.get(0).getFaPiaoDetails());//发票存储明细的订单
            total = CalcUtils.add(faPiaos.get(0).getTotal(), total);
            taxAmount = CalcUtils.add(faPiaos.get(0).getTaxAmount(), taxAmount);
            untotal = CalcUtils.add(faPiaos.get(0).getUntotal(), untotal);
        }
        faPiao = faPiaos.get(0);
        faPiao.setTotal(total);
        faPiao.setUntotal(untotal);
        faPiao.setTaxAmount(taxAmount);
        faPiao.setFaPiaoDetails(faPiaoDetails);
        faPiao.setUid(UuidUtils.randomString());
        faPiao.setBillCode(stringBuilder.toString());

        //ConfirmUtils.confirmAmount(faPiao);//校验数据

        //由于sqllite不支持多表联合插入数据，分开插入数据库
        List<FaPiao> list2 = new ArrayList();
        list2.add(faPiao);

        billSqliteDao.insertBill(list2);//插入主表的数据库
        billSqliteDetailDao.insertBillDetail(faPiao.getFaPiaoDetails());//插入明细表的数据库
        ajaxObject.setCode("0");
        ajaxObject.setMsg("合并成功！");
        return ajaxObject;
    }

    /**
     * @param billCodes
     * @return com.ilongsay.entity.AjaxObject
     * 优化后的单据合并功能的实现，可以实现全选合并，按客户名称相同的进行合并
     */
    @Override
    public AjaxObject mergeBillCode(String billCodes) {
        AjaxObject ajaxObject = new AjaxObject();
        //合并后的订单号的生成规则

        List<FaPiao> faPiaos = new ArrayList<>();

        Set<String> customName = new HashSet<>();
        FaPiao faPiao = null;
        String total = "0";
        String untotal = "0";
        String taxAmount = "0";

        String[] split = billCodes.split("\\,");
        for (String billCode : split) {//遍历单据
            faPiao = billSqliteDao.findBillByBillCode(billCode).get(0);
            faPiaos.add(faPiao);//得到选中所有的数据
            billSqliteDao.deleteBillByBillCode(billCode);//删除主表单号，因为sqlite不支持级联删除
            billSqliteDetailDao.deleteBillDetailsByBillCode(billCode);//删除明细单号
            customName.add(faPiao.getCustomName());//存储客户名称达到去重的目的
        }

        for (String s : customName) {//遍历客户名称
            FaPiao faPiao1 = null;
            List<FaPiaoDetail> faPiaoDetails = new ArrayList<>();
            DateUtils dateUtils = new DateUtils();
            String date = dateUtils.dateTime();
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append(date);//时间是因为速度太快导致相同，但是数据有问题
            stringBuilder.append("HB");

            for (int i = 0; i < faPiaos.size(); i++) {//遍历存储的数据

                if (faPiaos.get(i).getCustomName().equals(s)) {//如果存储的客户名称等于set里面的客户名称
                    faPiao1 = faPiaos.get(i);

                    //遍历list，将数据放入其中
                    faPiaoDetails.addAll(faPiaos.get(i).getFaPiaoDetails());
                }
            }

            faPiao1.getFaPiaoDetails().clear();

            //将list保存的元素保留
            faPiao1.setFaPiaoDetails(faPiaoDetails);

            //Set<String> billCode = new TreeSet<>();

            for (FaPiaoDetail faPiaoDetail1 : faPiao1.getFaPiaoDetails()) {

                faPiaoDetail1.setBillCode(stringBuilder.toString());
                faPiaoDetail1.setUpid(UuidUtils.randomString());

                //billCode.add(faPiaoDetail1.getOldCode());//将明细订单去重
            }

            ConfirmUtils.fixAmount(faPiao1);//重新校验一下数据
            ConfirmUtils.mergeNodes(faPiao1,true);//拆分后的明细备注问题 TODO:以后这里要设置为设置文件

            faPiao1.setBillCode(stringBuilder.toString());
            faPiao1.setUid(UuidUtils.randomString());
            //faPiao1.setNote(billCode.toString()+faPiao1.getNote());//将去重的订单号设置在备注中

            billSqliteDao.insertBill1(faPiao1);
            billSqliteDetailDao.insertBillDetail(faPiao1.getFaPiaoDetails());

        }


        ajaxObject.setCode("0");
        ajaxObject.setMsg(MSG_OK);
        return ajaxObject;
    }
}
